package com.dianrui.firemenmonitor.util;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * Created by lzy on 2017/11/9.
 * SharedPreferences封装
 */

@SuppressWarnings("ALL")
public class SharedUtil {;
    public static final String TOKEN = "TOKEN";//token

    public static final String MEMBER_TOKEN = "MEMBER_TOKEN";

    public static final String URL = "Url";

    public static final String NAME = "name";


    private SharedUtil()
    {
        throw new AssertionError();
    }

    public static String getPreferStr(String key) {//最常用的获取sp 类型为String的方法
        return getPreferStr(Static.CONTEXT, key);
    }

    public static String getPreferStr(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(PackageUtils.getPackageName(context), 0);
        return sp.getString(key, null);
    }

    public static String getPreferStr(String fileName, String key) {//获取制定sp里面的key值
        return getPreferStr(Static.CONTEXT, fileName, key);
    }

    private static String getPreferStr(Context context, String fileName, String key) {
        SharedPreferences sp = context.getSharedPreferences(fileName, 0);
        return sp.getString(key, null);
    }

    public static void setPreferStr(String key, String content) {//最常用的设置string类型的操作
        setPreferStr(Static.CONTEXT, key, content);
    }

    public static void setPreferStr(Context context, String key, String content)
    {
        SharedPreferences sp = context.getSharedPreferences(PackageUtils.getPackageName(context), 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, content);

        editor.commit();
    }

    private static void setPreferStr(String fileName, String key, String content) {//带有sp名称的设置
        setPreferStr(Static.CONTEXT, fileName, key, content);
    }

    private static void setPreferStr(Context context, String fileName, String key, String content) {
        SharedPreferences sp = context.getSharedPreferences(fileName, 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, content.trim());

        editor.commit();
    }

    public static void setPreferBool(String key, boolean content) {//最常用的
        setPreferBool(Static.CONTEXT, key, content);
    }

    public static void setPreferBool(Context context, String key, boolean content) {
        SharedPreferences sp = context.getSharedPreferences(PackageUtils.getPackageName(context), 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, content);

        editor.commit();
    }

    public static boolean getPreferBool(String key, boolean defValue) {//有默认值
        return getPreferBool(Static.CONTEXT, key, defValue);
    }

    public static boolean getPreferBool(Context context, String key, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(PackageUtils.getPackageName(context), 0);
        return sp.getBoolean(key, defValue);
    }

    public static int getPreferInt(String key, int defValue) {
        return getPreferInt(Static.CONTEXT, key, defValue);
    }

    public static int getPreferInt(Context context, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(PackageUtils.getPackageName(context), 0);
        return sp.getInt(key, defValue);
    }

    public static void setPreferInt(String key, int content) {
        setPreferInt(Static.CONTEXT, key, content);
    }

    public static void setPreferInt(Context context, String key, int content) {
        SharedPreferences sp = context.getSharedPreferences(PackageUtils.getPackageName(context), 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, content);

        editor.commit();
    }


    //删除操作开始
    public static void delPrefer(String key) {//删除一个key值
        delPrefer(Static.CONTEXT, key);
    }

    public static void delPrefer(SharedPreferences sp, String key) {//删除制定sp里面的key值
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);

        editor.commit();
    }

    public static void delPrefer(Context context, String key) {//删除一个key值 带Context
        SharedPreferences sp = context.getSharedPreferences(PackageUtils.getPackageName(context), 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);

        editor.commit();
    }

    public static void delAll(Context context) {//删除sp
        SharedPreferences sp = context.getSharedPreferences(PackageUtils.getPackageName(context), 0);
        SharedPreferences.Editor editor = sp.edit();

        editor.commit();
    }

    public static void delAllPrefer(SharedPreferences sp) {//删除制定sp
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();

        editor.commit();
    }
    //删除操作结束


    public static String getToken()
    {
        return getPreferStr(TOKEN);
    }

    public static void setToken(String token){
        setPreferStr(TOKEN, token);
    }

    public static void setUrl(String url){
        setPreferStr(URL, url);
    }
    public static String getUrl()
    {
        return getPreferStr(URL);
    }

    public static void setName(String name){
        setPreferStr(NAME, name);
    }
    public static String getName()
    {
        return getPreferStr(NAME);
    }

    public static void setMemberToken(String membername){
        setPreferStr(MEMBER_TOKEN, membername);
    }
    public static String getMemberToken()
    {
        return getPreferStr(MEMBER_TOKEN);
    }

}
